package com.lb.zookeeper.demo;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat;

/**
 * @Title: CuratorDemo
 * @Description: TODO
 * @Author lb
 * @Date 2019/04/04  18:07
 */
public class CuratorDemo {
    public static void main(String[] args) throws Exception {
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder().connectString("92.168.245.130:2181,192.168.245.131:2181,192.168.245.132:2181")
                .sessionTimeoutMs(4000).retryPolicy(new
                        ExponentialBackoffRetry(1000, 3)).namespace("curator").build();
        curatorFramework.start();
        //原生API中，必须逐层创建节点，父节点存在才能创建子节点
//        curatorFramework.create().creatingParentsIfNeeded().
//                withMode(CreateMode.PERSISTENT).forPath("/lb/node1","1".getBytes());
        Stat stat = new Stat();
        //删除,不需要逐层删除，原生API中，必须逐层删除节点
        curatorFramework.delete().deletingChildrenIfNeeded().forPath("/lb");
        //查看节点信息
//        curatorFramework.getData().storingStatIn(stat).forPath("/lb/node1");
        //修改节点信息
//        curatorFramework.setData().withVersion(stat.getVersion()).forPath("/lb/node1","xx".getBytes());

    }
}
